home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1249 / bubble.t < prev    next >
Text File  |  1997-04-18  |  2KB  |  78 lines

  1. %
  2. % "bubble.t" demonstrates the bubble sort algorithm
  3. %
  4. % The main routine reads from the terminal an array
  5. % of ten real numbers and calls the procedure Bubble
  6. % to sort them.
  7. %
  8. %   Sample program for the T Interpreter by:
  9. %
  10. %   Stephen R. Schmitt
  11. %   962 Depot Road
  12. %   Boxborough, MA 01719
  13. %
  14.  
  15. const TABLEN : int := 10                        % length of reals table
  16.  
  17. type TABLE : array[TABLEN] of real              % Table of reals type
  18.  
  19. var R : TABLE                                   % The table itself
  20. var i : int                                     % Table index
  21.  
  22. %
  23. % The sorting routine to sort in ascending order
  24. %
  25. procedure Bubble( var t : TABLE )
  26.  
  27.     var i, j : int
  28.     var temp : real
  29.  
  30.     for i := 0 ... TABLEN - 2 do                % the outer loop
  31.  
  32.         for j := i + 1 ... TABLEN - 1 do        % the inner loop
  33.  
  34.             if t[i] > t[j] then
  35.  
  36.                 temp := t[i]
  37.                 t[i] := t[j]
  38.                 t[j] := temp
  39.  
  40.             end if
  41.  
  42.         end for
  43.  
  44.     end for
  45.  
  46. end procedure
  47.  
  48. program
  49.  
  50.     var n : int
  51.  
  52.     put "Bubble Sort Demonstration Program"
  53.  
  54.     for i := 0 ... TABLEN - 1 do
  55.  
  56.         prompt "input real number no. " & intstr( i + 1, 2 ) & ":"
  57.         get R[i]
  58.  
  59.     end for
  60.  
  61.     Bubble( R )                                 % sort the array
  62.  
  63.     put "The sorted list from lowest to highest is:"
  64.  
  65.     for i := 0 ... TABLEN - 1 do
  66.  
  67.         put R[i]
  68.         n := ( i + 1 ) mod 5
  69.         
  70.         if n = 0 then
  71.      
  72.             put ""
  73.         
  74.         end if
  75.  
  76.     end for
  77.  
  78. end program